// 借助抛出异常会被捕获
class HttpException extends Error {
  constructor(msg = '服务器异常', errorCode = 10000, code = 200) {
    super()
    this.errorCode = errorCode
    this.code = code
    this.msg = msg
  }
}
class ServeError extends HttpException {
  constructor({ msg, errorCode = 500, code = 200 }) {
    super(msg, errorCode, code)
  }
}
class ParameterException extends HttpException {
  constructor(msg = '参数错误', errorCode = 10001, code = 200) {
    super(msg, errorCode, code)
  }
}

class AuthFailed extends HttpException {
  constructor(msg = '授权失败', errorCode = 10002, code = 200) {
    super(msg, errorCode, code)
  }
}
class NotFound extends HttpException {
  constructor(msg = '资源未找到', errorCode = 10003, code = 200) {
    super(msg, errorCode, code)
  }
}
class SuccessException extends HttpException {
  constructor({ msg = '请求成功', data = {}, errorCode = -1, code = 200 } = {}) {
    super(msg, errorCode, code)
    this.data = data
  }
}

const isCatchError = (err) => err instanceof HttpException

const isSuccess = (err) => err instanceof SuccessException
module.exports = {
  HttpException,
  ParameterException,
  AuthFailed,
  SuccessException,
  NotFound,
  ServeError,
  isCatchError,
  isSuccess,
}
